<?php

use app\modules\Admin\models\memberwalletrechargeModel;
use Illuminate\Database\Capsule\Manager as DB;
#[佣金明细管理]
class CommissionlogsController extends AcommonController
{
    public function init() {
        parent::init();
    }

    #[佣金明细列表]
    public function indexAction($returnData = FALSE)
    {
        if($this->isPost()){
            $params = $this->postData;
            $agentRewardModel = agentrewardModel::query()->select(['id','order_id','order_item_id','buyer_id','commission_order_id',
                                                                   'agent_id as daili_id','type','class','commission','original_commission',
                                                                   'commission_level','agent_level as level','commission_rules','status','created_at','updated_at', DB::raw ('"agent" AS model_name')]);
//            $gudongRerwardModel = gudongrewardModel::query()->select(['id','order_id','order_item_id','buyer_id','commission_order_id',
//                                                                      'gudong_id as daili_id','type','class','commission','original_commission',
//                                                                      'commission_level','agent_level as level','commission_rules','status','created_at','updated_at', DB::raw ('"gudong" AS model_name')]);
            $qudaiRewardModel = qudairewardModel::query()->select(['id','order_id','order_item_id','buyer_id','commission_order_id',
                                                                   'qudai_id as daili_id','type','class','commission','original_commission',
                                                                   'commission_level','area_level as level','commission_rules','status','created_at','updated_at', DB::raw ('"qudai" AS model_name')]);
            $tuikeRewardModel = tuikerewardModel::query()->select(['id','order_id','order_item_id','buyer_id','commission_order_id',
                                                                   'tuike_id as daili_id','type','class','commission','original_commission',
                                                                   'commission_level','tuike_level as level','commission_rules','status','created_at','updated_at', DB::raw ('"tuike" AS model_name')]);
            $twostageRewardModel = twostagerewardModel::query()->select(['id','order_id','order_item_id','buyer_id','commission_order_id',
                                                                   'twostage_id as daili_id','type','class','commission','original_commission',
                                                                   'commission_level','twostage_level as level','commission_rules','status','created_at','updated_at', DB::raw ('"twostage" AS model_name')]);
            $dianpufenhongRerwardModel = dianpurewardModel::query()->select(['id','order_id','order_item_id','buyer_id','commission_order_id',
                                                                               'manager_id as daili_id','type','class','commission','original_commission',
                                                                               'commission_level','dianpu_level as level','commission_rules','status','created_at','updated_at', DB::raw ('"dianpufenhong" AS model_name')]);
            if(isset($params['mode']) && $params['mode'] != 'all'){
                switch ($params['mode']){
                    case 'waiting':
                        $agentRewardModel = $agentRewardModel->where('status', agentrewardModel::COMMISSION_REWARD_STATUS_WAITING);
//                        $gudongRerwardModel = $gudongRerwardModel->where('status', gudongrewardModel::COMMISSION_REWARD_STATUS_WAITING);
                        $qudaiRewardModel = $qudaiRewardModel->where('status', qudairewardModel::COMMISSION_REWARD_STATUS_WAITING);
                        $tuikeRewardModel = $tuikeRewardModel->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_WAITING);
                        $twostageRewardModel = $twostageRewardModel->where('status', twostagerewardModel::COMMISSION_REWARD_STATUS_WAITING);
                        $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->where('status', dianpurewardModel::COMMISSION_REWARD_STATUS_WAITING);
                        break;
                    case 'accounted':
                        $agentRewardModel = $agentRewardModel->where('status', agentrewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
//                        $gudongRerwardModel = $gudongRerwardModel->where('status', gudongrewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
                        $qudaiRewardModel = $qudaiRewardModel->where('status', qudairewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
                        $tuikeRewardModel = $tuikeRewardModel->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
                        $twostageRewardModel = $twostageRewardModel->where('status', twostagerewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
                        $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->where('status', dianpurewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
                        break;
                    case 'cancel':
                        $agentRewardModel = $agentRewardModel->where('status', agentrewardModel::COMMISSION_REWARD_STATUS_CANCEL);
//                        $gudongRerwardModel = $gudongRerwardModel->where('status', gudongrewardModel::COMMISSION_REWARD_STATUS_CANCEL);
                        $qudaiRewardModel = $qudaiRewardModel->where('status', qudairewardModel::COMMISSION_REWARD_STATUS_CANCEL);
                        $tuikeRewardModel = $tuikeRewardModel->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_CANCEL);
                        $twostageRewardModel = $twostageRewardModel->where('status', twostagerewardModel::COMMISSION_REWARD_STATUS_CANCEL);
                        $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->where('status', dianpurewardModel::COMMISSION_REWARD_STATUS_CANCEL);
                        break;
                    case 'back':
                        $agentRewardModel = $agentRewardModel->where('status', agentrewardModel::COMMISSION_REWARD_STATUS_BACK);
//                        $gudongRerwardModel = $gudongRerwardModel->where('status', gudongrewardModel::COMMISSION_REWARD_STATUS_BACK);
                        $qudaiRewardModel = $qudaiRewardModel->where('status', qudairewardModel::COMMISSION_REWARD_STATUS_BACK);
                        $tuikeRewardModel = $tuikeRewardModel->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_BACK);
                        $twostageRewardModel = $twostageRewardModel->where('status', twostagerewardModel::COMMISSION_REWARD_STATUS_BACK);
                        $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->where('status', dianpurewardModel::COMMISSION_REWARD_STATUS_BACK);
                        break;
                }
            }
            if(isset($params['start_on']) && !empty($params['start_on'])){
                $agentRewardModel = $agentRewardModel->whereDate('created_at', '>=', $params['start_on']);
//                $gudongRerwardModel = $gudongRerwardModel->whereDate('created_at', '>=', $params['start_on']);
                $qudaiRewardModel = $qudaiRewardModel->whereDate('created_at', '>=', $params['start_on']);
                $tuikeRewardModel = $tuikeRewardModel->whereDate('created_at', '>=', $params['start_on']);
                $twostageRewardModel = $twostageRewardModel->whereDate('created_at', '>=', $params['start_on']);
                $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->whereDate('created_at', '>=', $params['start_on']);
            }
            if(isset($params['end_on']) && !empty($params['end_on'])){
                $agentRewardModel = $agentRewardModel->whereDate('created_at', '<=', $params['end_on']);
//                $gudongRerwardModel = $gudongRerwardModel->whereDate('created_at', '<=', $params['end_on']);
                $qudaiRewardModel = $qudaiRewardModel->whereDate('created_at', '<=', $params['end_on']);
                $tuikeRewardModel = $tuikeRewardModel->whereDate('created_at', '<=', $params['end_on']);
                $twostageRewardModel = $twostageRewardModel->whereDate('created_at', '<=', $params['end_on']);
                $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->whereDate('created_at', '<=', $params['end_on']);
            }
            if(isset($params['keywords']) && !empty($params['keywords'])){
                if(isset($params['keywordsType']) && !empty($params['keywordsType'])){
                    switch ($params['keywordsType']){
                        case 'daili_id':
                            $agentRewardModel = $agentRewardModel->where('agent_id', $params['keywords']);
//                            $gudongRerwardModel = $gudongRerwardModel->where('gudong_id', $params['keywords']);
                            $qudaiRewardModel = $qudaiRewardModel->where('qudai_id', $params['keywords']);
                            $tuikeRewardModel = $tuikeRewardModel->where('tuike_id', $params['keywords']);
                            $twostageRewardModel = $twostageRewardModel->where('twostage_id', $params['keywords']);
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->where('manager_id', $params['keywords']);
                            break;
                        case 'daili_phone':
                            $agentRewardModel = $agentRewardModel->whereIn('agent_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
//                            $gudongRerwardModel = $gudongRerwardModel->whereIn('gudong_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $qudaiRewardModel = $qudaiRewardModel->whereIn('qudai_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $tuikeRewardModel = $tuikeRewardModel->whereIn('tuike_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $twostageRewardModel = $twostageRewardModel->whereIn('twostage_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->whereIn('manager_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'order_id':
                            $agentRewardModel = $agentRewardModel->where('order_id', $params['keywords']);
//                            $gudongRerwardModel = $gudongRerwardModel->where('order_id', $params['keywords']);
                            $qudaiRewardModel = $qudaiRewardModel->where('order_id', $params['keywords']);
                            $tuikeRewardModel = $tuikeRewardModel->where('order_id', $params['keywords']);
                            $twostageRewardModel = $twostageRewardModel->where('order_id', $params['keywords']);
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->where('order_id', $params['keywords']);
                            break;
                        case 'order_sn':
                            $agentRewardModel = $agentRewardModel->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
//                            $gudongRerwardModel = $gudongRerwardModel->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $qudaiRewardModel = $qudaiRewardModel->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $tuikeRewardModel = $tuikeRewardModel->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $twostageRewardModel = $twostageRewardModel->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'buyer_id':
                            $agentRewardModel = $agentRewardModel->where('buyer_id', $params['keywords']);
//                            $gudongRerwardModel = $gudongRerwardModel->where('buyer_id', $params['keywords']);
                            $qudaiRewardModel = $qudaiRewardModel->where('buyer_id', $params['keywords']);
                            $tuikeRewardModel = $tuikeRewardModel->where('buyer_id', $params['keywords']);
                            $twostageRewardModel = $twostageRewardModel->where('buyer_id', $params['keywords']);
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->where('buyer_id', $params['keywords']);
                            break;
                        case 'buyer_phone':
                            $agentRewardModel = $agentRewardModel->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
//                            $gudongRerwardModel = $gudongRerwardModel->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $qudaiRewardModel = $qudaiRewardModel->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $tuikeRewardModel = $tuikeRewardModel->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $twostageRewardModel = $twostageRewardModel->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'goods_id':
                            $agentRewardModel = $agentRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_id', $params['keywords'])->pluck('id')->toArray());
//                            $gudongRerwardModel = $gudongRerwardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_id', $params['keywords'])->pluck('id')->toArray());
                            $qudaiRewardModel = $qudaiRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_id', $params['keywords'])->pluck('id')->toArray());
                            $tuikeRewardModel = $tuikeRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_id', $params['keywords'])->pluck('id')->toArray());
                            $twostageRewardModel = $twostageRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_id', $params['keywords'])->pluck('id')->toArray());
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_id', $params['keywords'])->pluck('id')->toArray());
                            break;
                        case 'goods_title':
                            $agentRewardModel = $agentRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_title', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
//                            $gudongRerwardModel = $gudongRerwardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_title', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $qudaiRewardModel = $qudaiRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_title', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $tuikeRewardModel = $tuikeRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_title', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $twostageRewardModel = $twostageRewardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_title', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            $dianpufenhongRerwardModel = $dianpufenhongRerwardModel->whereIn('order_item_id', orderitemModel::query()->where('goods_title', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                    }
                }
            }
//            $unionQuery = $agentRewardModel->unionAll($gudongRerwardModel)->unionAll($qudaiRewardModel)->unionAll($tuikeRewardModel)->unionAll($twostageRewardModel);
            $unionQuery = $agentRewardModel->unionAll($dianpufenhongRerwardModel)->unionAll($qudaiRewardModel)->unionAll($tuikeRewardModel)->unionAll($twostageRewardModel);
            $querySql = $unionQuery->toSql();

            $query = DB::table(DB::raw("($querySql) as unionSearch"))->mergeBindings($unionQuery->getQuery());

            $total = $query->count();

            $page = $this->get('page', 1);
            $limit = $this->get('rows', 10);
            $offset = ($page - 1) * $limit;

            $searchData = $query->orderBy('id', 'ASC')->offset($offset)->limit($limit)->get();
            if($searchData){
                foreach ($searchData as $key => &$item){
                    $item->order_info = \app\modules\Admin\models\orderModel::query()->where('id', $item->order_id)->select(['id', 'order_sn'])->first();
                    $item->order_item_info = \app\modules\Admin\models\orderItemModel::query()->where('id', $item->order_item_id)->select(['id', 'goods_id', 'goods_title', 'goods_sku_text', 'goods_num'])->first();
                    $item->buyer_info = memberModel::query()->where('id', $item->buyer_id)->select(['id', 'phone', 'nickname', 'realname'])->first();
                    $item->bonus_info = memberModel::query()->where('id', $item->daili_id)->select(['id', 'phone', 'nickname', 'realname'])->first();
                    switch ($item->status) {
                        case gudongrewardModel::COMMISSION_REWARD_STATUS_BACK:
                            $item->status_name = '已退回';
                            break;
                        case gudongrewardModel::COMMISSION_REWARD_STATUS_WAITING:
                            $item->status_name = '待入账';
                            break;
                        case gudongrewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED:
                            $item->status_name = '已入账';
                            break;
                        case gudongrewardModel::COMMISSION_REWARD_STATUS_CANCEL:
                            $item->status_name = '已取消';
                            break;
                        default:
                            $item->status_name = '';
                    }
                    $item->fenghong_type = '其他佣金';
                    switch ($item->class) {
                        case 'pingjifenhong':
                            $item->fenghong_type = '平级奖励';
                            break;
                        case 'jichafenhong':
                            $item->fenghong_type = '级差分红';
                            break;
                        case 'gudongfenhong':
                            $item->fenghong_type = '股东分红';
                            break;
                        case 'qudaifenhong':
                            $item->fenghong_type = '区域经销商分红';
                            break;
                        case 'tuikefenhong':
                            $item->fenghong_type = '推客佣金';
                            break;
                        case 'tuiguangfenhong':
                            $item->fenghong_type = '两级收益佣金';
                            break;
                        case 'dianpufenhong':
                            $item->fenghong_type = '店铺分红';
                            break;
                    }
//                    switch ($item->model_name){
//                        case 'agent':
//
//                            break;
//                        case 'gudong':
//                            break;
//                        case 'qudai':
//                            break;
//                        case 'tuike':
//                            break;
//                    }
                }
                unset($key);
                unset($item);
            }

            json(['total' => $total, 'rows' => $searchData]);
        }else{
            $id = $this->get('id', 0);
            $this->_view->assign('id', $id);
            return parent::indexAction($returnData); // TODO: Change the autogenerated stub
        }
    }
}